#include "common.h"

QFileInfoList getFileList(const QString& path)
{
    QFileInfoList result;

    QDir dir(path);

    dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks | QDir::Dirs | QDir::NoDotAndDotDot);
    dir.setSorting(QDir::Time | QDir::Reversed);

    QFileInfoList fileInfoList = dir.entryInfoList();

    foreach (QFileInfo fileInfo, fileInfoList)
    {
        if (fileInfo.isDir())
            result.append(getFileList(fileInfo.filePath()));
        else
            result.append(fileInfo);
    }
    return result;
}

void setBgColor(QWidget *parent, const QString &color)
{
    QPalette pal(parent->palette());
    pal.setColor(QPalette::Background, QColor(color));
    parent->setAutoFillBackground(true);
    parent->setPalette(pal);
}

void replaceQss(QString &qss, QString key, QString replace_str)
{
    int index, end_index;
    if (qss.contains(key))
    {
        index = qss.indexOf(key);
        end_index = qss.indexOf(";", index+1);
        qss.replace(index, end_index - index + 1, replace_str);
    } else
    {
        if (replace_str.contains(";color:"))
            qss.append(replace_str.right(replace_str.size() - 1));
        else
            qss.append(replace_str);
    }
}

bool delDoubleQuotationMark(QString path)
{
    QFile read_file(path);
    if (!read_file.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;

    QByteArray t = read_file.readAll();
    QString text = QString(t);

    read_file.close();

    if (text.contains("\"(#current_show#)\""))
        text.replace("\"(#current_show#)\"", "(#current_show#)");
    if (text.contains("\"(#playlist1#)\""))
        text.replace("\"(#playlist1#)\"", "(#playlist1#)");

    QFile write_file(path);
    if (!write_file.open(QIODevice::WriteOnly | QIODevice::Text))
        return false;
    write_file.write(text.toUtf8());

    write_file.close();

    return true;
}

bool addDoubleQuotationMark(QString path)
{
    QFile read_file(path);
    if (!read_file.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;

    QByteArray t = read_file.readAll();
    QString text = QString(t);

    read_file.close();

    if (text.contains("(#current_show#)"))
        text.replace("(#current_show#)", "\"(#current_show#)\"");
    if (text.contains("(#playlist1#)"))
        text.replace("(#playlist1#)", "\"(#playlist1#)\"");

    QFile write_file(path);
    if (!write_file.open(QIODevice::WriteOnly | QIODevice::Text))
        return false;

    write_file.write(text.toUtf8());

    write_file.close();

    return true;
}

